import React, { Component } from 'react';
import {
    View,
    Image,
    ScrollView,
    StyleSheet,
} from 'react-native'
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';

import moment from 'moment'
import * as fetch from '../../utils/fetch'
import Style from '../../utils/style/style'
import Loading from '../../views/loading';
import CellItem from '../../views/cellItem';
import CheckStateItem from '../../views/checkStateItem';
import CellTitleItem from '../../views/cellTitleItem';
import SerialsItem from '../../views/serialsItem';
import GoodItem from '../../views/goodItem';
import { resultSetailInitial } from '../../actions/CheckAction'
class ResultSerails extends Component {
    constructor(props) {
        super(props);
        this.props = {
            loading: false,
            inv_code: '',
            inv_time: '',
            state: '',
            itemTitle: '',
            goodsItem: {},
            dataList: null,
        }
    }

    componentDidMount() {
        const { navigation } = this.props;
        const inv_code = navigation.getParam("inv_code");
        const inv_time = navigation.getParam("inv_time");
        const state = navigation.getParam("state");
        const goodsItemStr = navigation.getParam("item");
        let itemTitle;
        switch (state) {
            case 1:
                itemTitle = "缺失的序列号";
                break;
            case 2:
                itemTitle = "冗余的序列号";
                break;
            case 4:
                itemTitle = "正常的序列号";
                break;
            default:
                break
        }
        params = {
            inv_code: inv_code,
            inv_time: inv_time,
            state: state,
            itemTitle: itemTitle,
            goodsItem: JSON.parse(goodsItemStr),
        }
        this.props.resultSetailInitial(params)
        // this.props.getResultSerails()
        // this._fetchData();
    }

    static navigationOptions = {
        title: '盘点结果',
    };

    _fetchData() {
    }
    _renderSerials(item, index, length) {
        const noBorder = index == length - 1 ? true : false;
        return (
            <SerialsItem value={item.BarCode} noBorder={noBorder} />
        )
    }

    render() {
        let time;
        if (this.props.inv_time) {
            time = moment(this.props.inv_time).format("YYYY-MM-DD HH:mm")
        }
        let goodsItem;
        if (this.props.goodsItem) {
            goodsItem = this.props.goodsItem
        }
        return (
            !this.props.inv_code || !this.props.inv_time ? null :
                <ScrollView style={styles.container}>
                    <View style={Style.gap} />
                    <CellItem title="盘点号" content={this.props.inv_code} />
                    <CellItem title="盘点时间" content={time} noBorder={true} />
                    <View style={Style.bottom} />
                    <GoodItem
                        source={goodsItem.images[0]}
                        name={goodsItem.Name}
                        key={goodsItem.name}
                        showAngle={false}
                        detailList={goodsItem.detailList}
                        pressItem={() => { return }}
                        noBorder={true}
                    />
                    {
                        goodsItem.serialList && goodsItem.serialList.length > 0 ?
                            <View>
                                <CellTitleItem title={this.props.itemTitle} />
                                <View style={styles.content}>
                                    {goodsItem.serialList.map((item, index) => this._renderSerials(item, index, goodsItem.serialList.length))}
                                </View>
                            </View>
                            : null
                    }
                    <View style={Style.bottom} />
                </ScrollView>
        )
    }
}
const styles = StyleSheet.create({
    container: {
        width: Style.width,
        height: Style.ViewHeightD,
        flex:1,
        backgroundColor: '#fff',
    },
    gapTop: {
        borderTopWidth: 1,
    },
})

export default connect(
    state => ({
        loading: state.ResultSerails.loading,
        inv_code: state.ResultSerails.inv_code,
        inv_time: state.ResultSerails.inv_time,
        state: state.ResultSerails.state,
        itemTitle: state.ResultSerails.itemTitle,
        goodsItem: state.ResultSerails.goodsItem,
        dataList: state.ResultSerails.dataList,
    }),
    dispatch => ({ resultSetailInitial: bindActionCreators(resultSetailInitial, dispatch) })
)(ResultSerails)